using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;
using C4F.DevKit.Contacts.Components;
using C4F.DevKit.Contacts;

namespace C4F.DevKit.Contacts.Controls
{
    /// <summary>
    /// This control provides some basic search functionality. It does not actually perform the search, but it raises events (and passes the query data) that will be used to perform the actual search.
    /// </summary>
    [ToolboxItem(true), ToolboxBitmap(typeof(ContactSearch))]
    public partial class ContactSearch : UserControl
	{
        /// <summary>
        /// Represents the method run when contacts are searched.
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
		public delegate void ContactsSearchEventHandler(object sender, ContactsSearchEventArgs e);

        /// <summary>
        /// This event is raised when searching contacts.
        /// </summary>
		public event ContactsSearchEventHandler ContactsSearch;

		#region Constructor

        /// <summary>
        /// Initializes the control
        /// </summary>
		public ContactSearch()
		{
			InitializeComponent();            
		}        

		#endregion Constructor

		#region Methods
                		
        #region Event Handlers

        private void btnSearch_Click(object sender, EventArgs e)
        {
            try
            {
                OnSearch();
            }
            catch (Exception ex)
            {
                DisplayException(ex);
            }
        }
        				        
		#endregion Event Handlers

        #region Private

        private void DisplayException(Exception ex)
        {
            if (ex != null)
                MessageBox.Show(ex.ToString());
        }

        private void OnSearch()
        {
            ContactsSearchEventArgs args = new ContactsSearchEventArgs();
            ContactsSearchFilter filter = new ContactsSearchFilter();

            filter.FirstName = txtFirst.Text;
            filter.LastName = txtLast.Text;
            filter.StreetAddress = txtAddress.Text;
            filter.City = txtCity.Text;
            filter.State = txtState.Text;
            filter.Country = txtCountry.Text;

            args.Filter = filter;

            if (ContactsSearch != null)
                ContactsSearch(this, args);
        }

        #endregion Private

		#endregion Methods

	}
}
